package com.xlh.jdbc.dao.impl;

import com.google.common.collect.Lists;
import com.xlh.jdbc.dao.RepositoryTemplateDAO;
import com.xlh.pojo.courseTest.CourseQuestionRepository;
import com.xlh.pojo.courseTest.CourseQuestionRepositoryDetails;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zhr 2019/12/2
 */
@Repository
public class RepositoryTemplateDAOImpl implements RepositoryTemplateDAO {

    @Override
    public List<CourseQuestionRepository> selectByChapterIds(JdbcTemplate jdbcTemplate, List<Long> chapterIds) {
        String sql = "select * from course_question_repository where chapter_id in ( " + StringUtils.join(chapterIds, ",") + " ) and del_mark = false";

        List<CourseQuestionRepository> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper(CourseQuestionRepository.class));
        return CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
    }

    @Override
    public List<CourseQuestionRepository> selectNoChapterId(JdbcTemplate jdbcTemplate, List<Long> courseIds) {
        String sql = "select * from course_question_repository where chapter_id is null and course_id in ( " + StringUtils.join(courseIds, ",") + " ) and del_mark = false";

        List<CourseQuestionRepository> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper(CourseQuestionRepository.class));
        return CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
    }

    @Override
    public List<CourseQuestionRepositoryDetails> selectDetails(JdbcTemplate jdbcTemplate, List<Long> questionIds) {
        String sql = "select * from course_question_repository_details where question_id in ( " + StringUtils.join(questionIds, ",") + " ) and del_mark = false";

        List<CourseQuestionRepositoryDetails> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper(CourseQuestionRepositoryDetails.class));
        return CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
    }
}
